package com.europe.business.europebusiness.ui.activity;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.activity.base.ActionBarActivity;
import com.europe.business.europebusiness.ui.fragment.AttFragment;
import com.europe.business.europebusiness.ui.fragment.CnFragment;
import com.europe.business.europebusiness.ui.fragment.EuFragment;
import com.europe.business.europebusiness.ui.fragment.HomeFragment;
import com.europe.business.europebusiness.ui.fragment.MyFragment;


import static com.europe.business.europebusiness.ui.activity.StatusBarUtil.setTranslucentStatus;

public class MainActivity extends ActionBarActivity {

    private LinearLayout mContainer;
    private RadioGroup mNavRg;
    private String[] mFragmentTag = {"HomeFragment", "EuFragment", "CnFragment", "AttFragment", "MyFragment"};
    Fragment curFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setTranslucentStatus(this);
        initFragment(0);
        setNavRadioButtonChecked(0);
    }


    @Override
    protected int setMainContent() {
        return R.layout.activity_main;
    }


    @Override
    protected void initView() {
        super.initView();
        setToolbarVisible(View.GONE);
        mContainer = findViewById(R.id.container);
        mNavRg = findViewById(R.id.nav_rg);
        mNavRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int index = -1;
                switch (checkedId) {
                    case R.id.home_rb:
                        index = 0;
                        break;
                    case R.id.eu_rb:
                        index = 1;
                        break;
                    case R.id.cn_rb:
                        index = 2;
                        break;
                    case R.id.att_rb:
                        index = 3;
                        break;
                    case R.id.my_rb:
                        index = 4;
                        break;
                }
                initFragment(index);
            }
        });
    }

    private void initFragment(int index) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        Fragment fragment = fragmentManager.findFragmentByTag(mFragmentTag[index]);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (null == fragment) {

            switch (index) {
                case 0:
                    fragment = HomeFragment.newInstance();
                    fragmentTransaction.add(R.id.container, fragment, mFragmentTag[index]);
                    break;
                case 1:
                    fragment = EuFragment.newInstance();
                    fragmentTransaction.add(R.id.container, fragment, mFragmentTag[index]);
                    break;
                case 2:
                    fragment = CnFragment.newInstance();
                    fragmentTransaction.add(R.id.container, fragment, mFragmentTag[index]);
                    break;
                case 3:
                    fragment = AttFragment.newInstance();
                    fragmentTransaction.add(R.id.container, fragment, mFragmentTag[index]);
                    break;
                case 4:
                    fragment = MyFragment.newInstance();
                    fragmentTransaction.add(R.id.container, fragment, mFragmentTag[index]);
                    break;
            }
        }
        if (null != curFragment) {
            fragmentTransaction.hide(curFragment);
        }
        curFragment = fragment;
        fragmentTransaction.show(fragment);
        fragmentTransaction.commit();
    }

    private void setNavRadioButtonChecked(int index) {
        int viewId = -1;
        switch (index) {
            case 0:
                viewId = R.id.home_rb;
                break;
            case 1:
                viewId = R.id.eu_rb;
                break;
            case 2:
                viewId = R.id.cn_rb;
                break;
            case 3:
                viewId = R.id.att_rb;
                break;
            case 4:
                viewId = R.id.my_rb;
                break;
        }
        if (viewId == -1) {
            return;
        }
        ((RadioButton) findViewById(viewId)).setChecked(true);
    }
}
